package com.tebiecloud.goods.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tebiecloud.act.client.model.entity.ActGoodsRef;
import com.tebiecloud.act.client.model.vo.ActListVO;
import com.tebiecloud.act.client.model.vo.StoreVO;
import com.tebiecloud.common.constants.ResultEnum;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.PageResult;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.security.SecurityHelper;
import com.tebiecloud.common.security.SecurityUserDetails;
import com.tebiecloud.common.utils.DistanceUtils;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.goods.client.model.entity.Goods;
import com.tebiecloud.goods.client.model.entity.GoodsCollect;
import com.tebiecloud.goods.client.model.entity.GoodsSpecification;
import com.tebiecloud.goods.client.model.vo.GoodsListVO;
import com.tebiecloud.goods.server.mapper.GoodsCollectMapper;
import com.tebiecloud.goods.server.mapper.GoodsMapper;
import com.tebiecloud.goods.server.mapper.GoodsSpecificationMapper;
import com.tebiecloud.goods.server.service.GoodsCollectService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import com.tebiecloud.goods.server.service.feign.ActGoodsRefServiceClient;
import com.tebiecloud.goods.server.service.feign.ActServiceClient;
import com.tebiecloud.goods.server.service.feign.StoreServiceClient;
import com.tebiecloud.store.client.model.entity.Store;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏表 服务实现类
 * @author tebie
 * @date 2019-09-23
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
@SuppressWarnings("all")
public class GoodsCollectServiceImpl extends BaseServiceImpl<GoodsCollectMapper, GoodsCollect> implements GoodsCollectService {

    @Autowired
    GoodsCollectMapper goodsCollectMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;

    @Autowired
    ActGoodsRefServiceClient actGoodsRefServiceClient;

    @Autowired
    ActServiceClient actServiceClient;

    @Autowired
    StoreServiceClient storeServiceClient;

    @Override
    public IPage<GoodsListVO> selectGoodsCollectList(int page, int limit, Double lng, Double lat) {
        //当前登录人
        SecurityUserDetails securityUserDetails = SecurityHelper.getUser();
        long userId = securityUserDetails.getUserId();
        //获取收藏列表
        QueryWrapper<GoodsCollect> goodsCollectQueryWrapper = new QueryWrapper<>();
        goodsCollectQueryWrapper.eq("user_id",userId);
        goodsCollectQueryWrapper.orderByDesc("ct");
        IPage<GoodsCollect> goodsCollectList = goodsCollectMapper.selectPage(new PageParams(page,limit),goodsCollectQueryWrapper);
        PageResult<GoodsListVO> goodsListPage = new PageResult<>(goodsCollectList);
        List<GoodsListVO> goodsListVOList = new ArrayList<>();
        if(goodsCollectList.getRecords()!=null && goodsCollectList.getRecords().size()>0){
            for(GoodsCollect goodsCollect:goodsCollectList.getRecords()){
                GoodsListVO goodsListVO = new GoodsListVO();
                //查询商品信息
                QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
                goodsQueryWrapper.eq("goods_id",goodsCollect.getGoodsId());
                Goods goods = goodsMapper.selectOne(goodsQueryWrapper);
                if(goods==null){
                    log.info(">>minjie 查询不到商品");
                    continue;
                }
                if(goodsCollect.getActId()!=null){
                    //查询活动信息
                    ResultBody<ActListVO> actInfoVOResultBody = actServiceClient.selectActByActIdAndGoodsId(goodsCollect.getActId(),goods.getGoodsId());
                    if(actInfoVOResultBody!=null && actInfoVOResultBody.getCode() == ResultEnum.OK.getCode()){
                        ActListVO actListVO = actInfoVOResultBody.getData();
                        goodsListVO.setActId(actListVO.getActId());
                        goodsListVO.setGoodsId(goodsCollect.getGoodsId());
                        goodsListVO.setGoodsName(actListVO.getActName());
                        goodsListVO.setSpecName(actListVO.getSpecName());
                        goodsListVO.setGoodsType(2);
                        goodsListVO.setMainPic(actListVO.getMainPic());
                        goodsListVO.setPrice(actListVO.getPrice());
                        goodsListVO.setRetailPrice(actListVO.getRetailPrice());
                        goodsListVO.setShelfTime(actListVO.getShelfTime());
                        goodsListVO.setStartTime(actListVO.getStartTime());
                        goodsListVO.setEndTime(actListVO.getEndTime());
                        goodsListVO.setPurchaseStatus(actListVO.getPurchaseStatus());
                    }else {
                        log.info(">>minjie 查询没有活动");
                    }
                }else{
                    //如果不是活动商品
                    goodsListVO.setGoodsId(goods.getGoodsId());
                    goodsListVO.setGoodsName(goods.getName());
                    goodsListVO.setMainPic(goods.getMainPic());
                    //查询规格列表
                    QueryWrapper<GoodsSpecification> goodsSpecificationQueryWrapper = new QueryWrapper<>();
                    goodsSpecificationQueryWrapper.eq("goods_id",goods.getGoodsId());
                    goodsSpecificationQueryWrapper.eq("down",0);
                    goodsSpecificationQueryWrapper.eq("deleted",0);
                    List<GoodsSpecification> goodsSpecificationList = goodsSpecificationMapper.selectList(goodsSpecificationQueryWrapper);
                    if(goodsSpecificationList.size()>0){
                        int j = 0;//获取价格最小的规格
                        int stockQty = 0; //计算库存
                        for(int i=0;i<goodsSpecificationList.size();i++){
                            if(stockQty!=-99)stockQty = stockQty + goodsSpecificationList.get(i).getStockQty();
                            if(i==0)j=0;
                            if(goodsSpecificationList.get(i).getPrice()<goodsSpecificationList.get(j).getPrice()){
                                j = i;
                            }
                        }
                        goodsListVO.setSpecName(goodsSpecificationList.get(j).getName());
                        goodsListVO.setGoodsType(1);
                        goodsListVO.setPrice(goodsSpecificationList.get(j).getPrice());
                        goodsListVO.setRetailPrice(goodsSpecificationList.get(j).getRetailPrice());
                        if(stockQty==0){
                            goodsListVO.setPurchaseStatus(3);
                        }else{
                            goodsListVO.setPurchaseStatus(2);
                        }
                    }else{
                        log.info(">>minjie 查询没有规格");
                    }

                }
                //获取门店信息
                ResultBody<List<Store>> storeResultBody = storeServiceClient.selectStoreList(goods.getStoreId().toString());
                if(storeResultBody!=null && storeResultBody.getCode() == ResultEnum.OK.getCode()){
                    List<Store> storeList = storeResultBody.getData();
                    //计算距离
                    if(storeList!=null && storeList.size()>0){
                        if(lat!=null && lng!=null && lat!=-1 && lng!=-1){
                            String distance = DistanceUtils.getDistance(lng,lat,storeList.get(0).getLng(),storeList.get(0).getLat());
                            goodsListVO.setDistance(distance);
                        }
                        goodsListVO.setStoreName(storeList.get(0).getName());
                    }
                }
                goodsListVOList.add(goodsListVO);
            }
        }
        //最后赋值给分页对象
        goodsListPage.setRecords(goodsListVOList);
        return goodsListPage;
    }
}
